﻿using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using UploadClient.Framework;
using UploadClient.View;
using UploadClient.ViewModel;

namespace UploadClient
{
    public delegate void CommandLineArgsHandler(string[] args);

    /// <summary>
    /// Interaktionslogik für "App.xaml"
    /// </summary>
    public partial class App : Application
    {
        public static event CommandLineArgsHandler CommandLineArgs;

        //Override the OnStartup() method
        protected override void OnStartup(StartupEventArgs e)
        {
            // ToDo: if e.args == "-t" -> start program in tray!
            // plugin autostart sets this if it´s set in the config?! 

            base.OnStartup(e);

            var rd = new ResourceDictionary { Source = new Uri("pack://application:,,,/View/WindowRessources.xaml") };

            this.Resources.MergedDictionaries.Add(rd);

            var mainWindowView = new MainWindowView();
            mainWindowView.Show();

            this.MainWindow = mainWindowView;

            if (CommandLineArgs != null)
                CommandLineArgs.Invoke(e.Args);
        }

        protected override void OnActivated(EventArgs e)
        {
            if (MainWindow is MainWindowView && MainWindow != null && MainWindow.DataContext == null)
            {
                // ToDo: add IHost to DefaultCommunication(?) and add Window.Handle
                this.MainWindow.DataContext = new MainWindowViewModel(new DefaultCommunication(new Options(true), new ConnectionSettings(true)));
            }

            base.OnActivated(e);
        }

        //Method used to handle command line arguments
        public void HandleCommandLine(ReadOnlyCollection<string> e)
        {
            //Code to handle command line arguments from other instances
            string[] args = new string[e.Count()];
            e.CopyTo(args, 0);

            CommandLineArgs(args);
        }
    }

}
